﻿using Manon.Admin.ApplicationCore.Common;
using Manon.Admin.ApplicationCore.Infrastructure;
using Manon.Admin.Web.ApplicationCore.Dtos.Company;
using Manon.Admin.Web.ApplicationCore.Entities.Company;
using Manon.Admin.Web.ApplicationCore.Interfaces;
using Manon.Autofac;
using Manon.Core.Commons;
using Manon.Repository;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Manon.Admin.Web.ApplicationCore.Services
{
    public class CompanyService : ICompanyService, ISingletonDependency
    {
        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<GetCompanyInfoOutput> GetCompanyInfo()
        {

            var logger = Resolver.Resolve<ILogger<CompanyService>>();
            try
            {


                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();

                var result = await unitOfWork.GetRepository<Company>().GetFirstOrDefaultAsync<GetCompanyInfoOutput>();
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("获取公司信息异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 保存公司信息
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SaveCompanyInfo(SaveCompanyInfoInput Input)
        {
            var logger = Resolver.Resolve<ILogger<CompanyService>>();
            try
            {
                var identityUser = Resolver.Resolve<IIdentityUser>();
                var unitOfWork = Resolver.Resolve<IUnitOfWork<AdminDbContext>>();
                var repos = unitOfWork.GetRepository<Company>();


                var result = await repos.FindAsync(Input.Id);
                if (result == null)
                {
                    result = new Company();
                    result.Address = Input.Address;
                    result.BriefIntroduction = Input.BriefIntroduction;
                    result.CompanyName = Input.CompanyName;
                    result.Contact = Input.Contact;
                    result.CreateTime = DateTime.Now;
                    result.UpdateTime = DateTime.Now;
                    result.CreateBy = identityUser.UserId;
                    result.UpdateBy = identityUser.UserId;
                    await repos.InsertAsync(result);
                }
                else
                {
                    result.Address = Input.Address;
                    result.BriefIntroduction = Input.BriefIntroduction;
                    result.CompanyName = Input.CompanyName;
                    result.Contact = Input.Contact;
                    result.UpdateTime = DateTime.Now;
                    result.UpdateBy = identityUser.UserId;
                    repos.Update(result);
                }
                await unitOfWork.SaveChangesAsync();
                return Result.Successed("保存成功!");
            }
            catch (Exception ex)
            {
                logger.LogError($"保存公司信息异常，入参input={JsonConvert.SerializeObject(Input)}", ex);
                throw;
            }
        }
    }
}
